

//
//  myMovieViewController.m
//  SELF 豆瓣
//
//  Created by dllo on 15/11/7.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "myMovieViewController.h"
#import "DataBaseSingleton.h"
#import "movieShouCang.h"
@interface myMovieViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *mytablew;
@property(nonatomic,retain)NSMutableArray *allArray;
@property(nonatomic,retain)movieShouCang *myshou;
@property(nonatomic,retain)NSMutableArray *tableArray;


@end

@implementation myMovieViewController
- (void)dealloc
{
    [_mytablew release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    
    self.mytablew = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    [self.view addSubview:self.mytablew];
    [_mytablew release];
    self.mytablew.delegate = self;
    self.mytablew.dataSource = self;
    [self.mytablew registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    //创建导航栏右侧编辑按钮
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
    
    
    
    
    self.allArray = [NSMutableArray array];
    self.tableArray = [NSMutableArray array];
    
    NSString *name = [[NSUserDefaults standardUserDefaults]objectForKey:@"name"];
    [[DataBaseSingleton shareDataBase]openDB];
    self.allArray = [[DataBaseSingleton shareDataBase]selectAllShouCang];
    for (movieShouCang *cang in self.allArray) {
        if ([cang.person isEqualToString:name])
        {
            self.myshou = [[movieShouCang alloc]init];
            self.myshou = cang;
            [self.tableArray addObject:self.myshou];
        }
        
    }
    
}

//删除逻辑

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    movieShouCang *moSHOUcang = [[movieShouCang alloc]init];
    moSHOUcang = [self.tableArray objectAtIndex:indexPath.row];
    
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //        [[DataBaseSingleton shareDataBase]openDB];
        [[DataBaseSingleton shareDataBase]deleteCollectings:moSHOUcang.movieName name:moSHOUcang.person];
        [self.tableArray removeObject:moSHOUcang];
    }
    
    [self.mytablew reloadData];
}

#pragma mark--系统编辑按钮触发的方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    
    //设置tableview编辑状态,随按钮状态改变而改变
    [self.mytablew setEditing:editing animated:animated];
    
    
}



#pragma mark--设置跨区不能移动
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    //如果移动的源section跟目标section 那么允许移动
    if (sourceIndexPath.section == proposedDestinationIndexPath.section)
    {
        
        return proposedDestinationIndexPath;
        
        
    }
    else
        
        return   sourceIndexPath;
    
}


#pragma mark--移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    
    
    
    //交换数组两个对象的位置
    [self.tableArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    
    
    
}






//区

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return   self.tableArray.count;
    
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellid = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    
    movieShouCang *shoucang  = [[movieShouCang alloc]init];
    shoucang = [self.tableArray objectAtIndex:indexPath.row];
    cell.textLabel.text = shoucang.movieName;
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
